//
//  DetailHouseTypeViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/4/21.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//   楼盘房型

#import "DetailHouseTypeViewController.h"
#import "HouseTypeCell.h"
#import "HomeNewSiteCellModel.h"
#import "config.h"
#import "NodataCell.h"
#import "Connect.h"
#import "HouseListDataManage.h"
#import "HouseTypeCellModel.h"
#import "MJRefresh.h"
#import "HouseTypeViewController.h"

#import "DetailMainTableViewController.h"

#define LABWIDTH  ([UIScreen mainScreen].bounds.size.width/5)
#define  SCALE UIScreenWidth/320

@interface DetailHouseTypeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *lab;
@property (weak, nonatomic) IBOutlet UILabel *redLab;
//全部
@property (weak, nonatomic) IBOutlet UIButton *firstBtn;
//现房
@property (weak, nonatomic) IBOutlet UIButton *secondBtn;
//期房
@property (weak, nonatomic) IBOutlet UIButton *thirdBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
- (IBAction)clickBtn:(UIButton *)sender;
//保存选中的按钮
@property (strong, nonatomic) UIButton *seletedBtn;

//保存列表的数据
@property (nonatomic,strong)NSArray *dataArray;
//保存的列表以为的数据
@property (strong, nonatomic) NSArray *otherDataArr;
@property (nonatomic,strong)NodataCell *nodataCell;
//@property (nonatomic,strong) NSMutableArray *selectArrM;
@property (strong, nonatomic) HouseListDataManage *dataManage;
//3:现房
@property (copy, nonatomic) NSString *ForwardDelivery;

@end

@implementation DetailHouseTypeViewController
@synthesize dataArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
    [self.titleView setMianTitle:@"楼盘房型"];
    self.titleView.doneBtn.hidden = YES;
    [self clickBtn:self.firstBtn];
    [self refreshDataWithIsForwardDelivery:@"3"];
    self.ForwardDelivery  = @"3";
   
    
    
}
#pragma mark 下拉加载
-(void)addLegendHeaderRefresh
{
     [self.collectionView addLegendFooterWithRefreshingBlock:^{
      [self loadMoreData];
      
  }];
}

#pragma mark 上拉刷新
-(void)addPulRefresh
{
//    [self.collectionView addLegendFooterWithRefreshingBlock:^{
//        
//    }];
    
    [self.collectionView addLegendHeaderWithRefreshingBlock:^{
        [self refreshDataWithIsForwardDelivery:self.ForwardDelivery];
    }];
}
-(void)loadMoreData
{
    NSString *preisesId = @"15671";
    NSString *isForwardDelivery = @"3";
    NSString *consumerId = @"73";
    
    
    __block typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSArray *arr  =[self.dataManage loadMoreDataWithPremisesId:preisesId WithIsForwardDelivery:isForwardDelivery WithConsumerId:consumerId];
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataArray];
        [array addObjectsFromArray:arr];
        self.dataArray = [NSArray arrayWithArray:array];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
           [self.collectionView.footer endRefreshing];
            
            if (arr.count == 0)
            {
                [self.collectionView.footer noticeNoMoreData];
            }

            self.lab.text = [self.otherDataArr objectAtIndex:2];
            self.textView.text = [self.otherDataArr objectAtIndex:1];
            
            if (dataArray==nil) {//网络出错
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(dataArray.count==0){//没数据
                [bself.nodataCell setState:nodataCellStateNodata];
            }else{//有数据
                
                [bself.nodataCell setState:nodataCellStateNorMal];
            }
            
            [bself.collectionView reloadData];
            
        });
    });

}

#pragma mark 请求数据 isForwardDelivery = 3 全部，0现房，1期房
-(void)refreshDataWithIsForwardDelivery:(NSString *)isForwardDelivery
{

    NSString *preisesId = @"15671";
    NSString *consumerId = @"73";
    [self.nodataCell setState:nodataCellStateLoading];
    
    __block typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =[self.dataManage refreshDataWithPremisesId:preisesId WithIsForwardDelivery:isForwardDelivery WithConsumerId:consumerId];
        
        self.dataArray = mod.dataMod;
        self.otherDataArr = [NSArray arrayWithArray:mod.otherDataMod];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView.header endRefreshing];
            
            self.lab.text = [self.otherDataArr objectAtIndex:2];
            self.textView.text = [self.otherDataArr objectAtIndex:1];
            
            NSString *numStr ;
            if ([[self.otherDataArr objectAtIndex:0] isKindOfClass:[NSNull class]] || [self.otherDataArr objectAtIndex:0] == nil)
            {
                    numStr = @"0";
            }
            else
            {
                numStr = [NSString stringWithFormat:@"%@",[self.otherDataArr objectAtIndex:0]];
            }
            if ([isForwardDelivery isEqualToString:@"3"])
            {
            [self.firstBtn setTitle:[NSString stringWithFormat:@"全部(%@)",numStr] forState:UIControlStateSelected];

            }else if ([isForwardDelivery isEqualToString:@"0"])
            {
                 [self.secondBtn setTitle:[NSString stringWithFormat:@"现房(%@)",numStr] forState:UIControlStateSelected];
            }else
            {
                [self.thirdBtn setTitle:[NSString stringWithFormat:@"期房(%@)",numStr] forState:UIControlStateSelected];
            }
            if(self.dataArray == 0)
            {
                [self.collectionView.footer noticeNoMoreData];
            }

            [bself.collectionView reloadData];
        });
    });

}

-(void)createCollectionView
{
    self.dataManage = [[HouseListDataManage alloc]init];
    
    _collectionView.frame = CGRectMake(0, 150, UIScreenWidth, UIScreenHeight - 150);
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = CGSizeMake(UIScreenWidth/2-1, 125*(UIScreenWidth-1)/175);
    layout.minimumInteritemSpacing = 0;
    _collectionView.collectionViewLayout = layout;
    
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HouseTypeCell" bundle:nil] forCellWithReuseIdentifier:@"houseTypeCell"];
    
    [self addLegendHeaderRefresh];
    [self addPulRefresh];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.dataArray.count  == 0)
    {
        UICollectionViewCell *cell = [[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, self.collectionView.frame.origin.y, self.collectionView.frame.size.width, self.collectionView.frame.size.width)];
        [self.collectionView removeFooter];
        return cell;
    }
    else
    {
        HouseTypeCell *cell =  [_collectionView dequeueReusableCellWithReuseIdentifier:@"houseTypeCell" forIndexPath:indexPath];
        cell.model = [self.dataArray objectAtIndex:indexPath.row];
        return cell;

    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

{
//    HouseTypeViewController2 *vc = [[HouseTypeViewController2 alloc]init];
    HouseTypeViewController *vc = [[HouseTypeViewController alloc]init];
    HouseTypeCellModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if (model.housingId >= 0)
    {
         vc.premiseshousingId = model.housingId;
    }else
    {
        vc.premiseshousingId = 0;
    }
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

- (IBAction)clickBtn:(UIButton *)sender
{
    
    self.seletedBtn.selected = NO;
    sender.selected = YES;
    self.seletedBtn = sender;
    
    self.redLab.highlighted = YES;
    CGFloat senderCenterX= sender.center.x;
    self.redLab.center = CGPointMake(senderCenterX, 33);
    if (sender.tag ==  0 )//全部
    {
        [self refreshDataWithIsForwardDelivery:@"3"];
        self.ForwardDelivery = @"3";
    }
    else if(sender.tag == 1)//现房
    {
        [self refreshDataWithIsForwardDelivery:@"0"];
        self.ForwardDelivery = @"0";
    }
    else//期房
    {
        [self refreshDataWithIsForwardDelivery:@"1"];
        self.ForwardDelivery = @"1";
    }
    
}

//-(void)btnOnclick:(UIButton *)btn
//{
//    NSLog(@"----");
//}
@end
